package com.yamed.bus.service.impl;

import com.yamed.bus.dal.dao.PromoteApkConfigDao;
import com.yamed.bus.dal.dao.PromoteIpCountDao;
import com.yamed.bus.dal.entity.PromoteApkConfig;
import com.yamed.bus.dal.entity.PromoteIpCount;
import com.yamed.bus.service.PromoteApkService;
import com.yamed.bus.service.bloom.SimpleBloomFilter;
import com.yamed.bus.web.vo.PromoteApkCountVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


@Service
public class PromoteApkServiceImpl implements PromoteApkService {
    @Resource
    private PromoteApkConfigDao promoteApkConfigDao;
    @Resource
    private PromoteIpCountDao promoteIpCountDao;
    @Resource
    private SimpleBloomFilter simpleBloomFilter;

    /**
     * 添加推广链接配置
     * @param promoteApkConfig
     * @return
     */
    @Override
    public Integer addAppConfig(PromoteApkConfig promoteApkConfig) {
        return promoteApkConfigDao.saveRecord(promoteApkConfig);
    }

    /**
     * 更新推广链接配置
     *
     * @param promoteApkConfig
     * @return
     */
    @Override
    public Integer updateAppConfig(PromoteApkConfig promoteApkConfig) {
        return promoteApkConfigDao.updateRecord(promoteApkConfig);
    }

    /**
     * 查询推广链接配置
     * @param promoteCode
     * @param ip
     * @return
     */
    @Override
    public PromoteApkConfig getPromoteAppConfig(String promoteCode, String ip) {
        PromoteApkConfig promoteApkConfig = promoteApkConfigDao.getByPromoteCode(promoteCode);
        if(Objects.isNull(promoteApkConfig)){
            throw new RuntimeException("配置不存在或已失效！");
        }
        Long refId = promoteApkConfig.getId();
//        if(!simpleBloomFilter.contains(ip)){
//            addClickCount(ip, refId, promoteCode);
//        }
        Boolean isIpExists = isIpExists(ip, refId, Boolean.FALSE);
        if(Objects.isNull(isIpExists)){
            addClickCount(ip, refId, promoteCode);
        }
        return promoteApkConfig;
    }

    /**
     * 获取产品推广情况
     * @param promoteCode
     * @return
     */
    @Override
    public List<PromoteApkConfig> getList(String promoteCode) {
        return promoteApkConfigDao.getList(promoteCode);
    }

    /**
     * 查询推广配置转化列表
     *
     * @param promoteCode
     * @return
     */
    @Override
    public List<PromoteApkCountVO> getTransList(String promoteCode) {
        return promoteApkConfigDao.getTransList(promoteCode);
    }

    /**
     * 添加下载次数
     * @param id
     * @param ip
     * @return
     */
    @Override
    public void updateDownloadCount(Long id, String ip) {
        Boolean isExists = isIpExists(ip, id, Boolean.TRUE);
        if(Objects.isNull(isExists)){
            //异常数据 不统计
            return ;
        }
        if(!isExists) {
            promoteIpCountDao.addDownloadCount(id, ip);
            promoteApkConfigDao.addDownloadCount(id);
        }
    }

    /*************************************  私有方法 **************************************/
    /**
     * 添加点击统计
     * @param ip
     * @param refId
     * @param promoteCode
     */
    private void addClickCount(String ip, Long refId, String promoteCode){
        PromoteIpCount promoteIpCount = new PromoteIpCount();
        promoteIpCount.setClickCount(1);
        promoteIpCount.setRefId(refId);
        promoteIpCount.setIpAddress(ip);
        promoteIpCount.setPromoteCode(promoteCode);
        promoteIpCountDao.saveRecord(promoteIpCount);
        promoteApkConfigDao.addClickCount(refId);
    }


    /**
     * 判断该ip是否已经访问或者下载
     * @param ip
     * @param refId
     * @param isDownload
     * @return
     */
    private Boolean isIpExists(String ip, Long refId, Boolean isDownload){
        PromoteIpCount promoteIpCount = promoteIpCountDao.getPromoteIpCount(refId, ip);
        if(Objects.isNull(promoteIpCount)){
            return null;
        }
        if(isDownload && promoteIpCount.getDownloadCount() == 0){
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
